/*
 * Create Author  : jinpeng.chen
 * Create Date    : 2017-01-10
 * Project        : springboot-chapter5
 * File Name      : PersonServiceImpl.java
 *
 * Copyright (c) 2010-2015 by Shanghai HanTao Information Co., Ltd.
 * All rights reserved.
 *
 */
package com.charles.app.service;

import com.charles.app.dao.PersonRepository;
import com.charles.app.domain.Person;
import com.charles.app.dto.PersonDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
 * 功能描述:  <p>
 *
 * @author : jinpeng.chen <p>
 * @version 1.0 2017-01-10
 * @since springboot-chapter5 1.0
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonRepository personRepository;

    @Override
    public Optional<List<PersonDTO>> findByNameLike(String name) {
        return Optional.of(personRepository.findByNameLike("%" + name + "%")).map(new Function<List<Person>, List<PersonDTO>>() {
            @Override
            public List<PersonDTO> apply(List<Person> persons) {
                List<PersonDTO> list = new ArrayList<PersonDTO>();
                for (Person p : persons) {
                    PersonDTO personDTO = new PersonDTO();
                    BeanUtils.copyProperties(p, personDTO);
                    list.add(personDTO);
                }
                return list;
            }
        });
    }
}
